package cn.db101.kaoqinserver.service;

import cn.db101.kaoqinserver.domain.Check;
import cn.db101.kaoqinserver.domain.CheckStat;
import cn.db101.kaoqinserver.domain.Classes;
import cn.db101.kaoqinserver.domain.User;
import cn.db101.kaoqinserver.mapper.ClassesMapper;
import cn.db101.kaoqinserver.mapper.UserMapper;
import cn.db101.kaoqinserver.model.request.ClassesParam;
import cn.db101.kaoqinserver.model.request.UserParam;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ClassesMapper classesMapper;

    private Map<String, String> captchaMap = new HashMap<>();

    public User getUserByLoginName(String loginName) {

        return userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getLoginName, loginName));
    }

    /**
     * 更新用户头像
     */
    public void updateUserAvatar(User user) {

        userMapper.update(null, new LambdaUpdateWrapper<User>()
                .eq(User::getId, user.getId())
                .set(User::getAvatar, user.getAvatar())
        );
    }

    public User getUserById(int loginId) {

        return userMapper.selectById(loginId);
    }

    public Page<Classes> classesPage(ClassesParam classesParam) {

        return classesMapper.selectPage(classesParam, new LambdaQueryWrapper<Classes>());
    }

    public Page<Classes> userPage(UserParam userParam) {

        return userMapper.selectPage(userParam, new LambdaQueryWrapper<User>());
    }

    public void bindClass(User user) {

        userMapper.update(null, new LambdaUpdateWrapper<User>()
                .set(User::getClassesId, user.getClassesId())
                .eq(User::getId, user.getId())
        );
    }

    public void bindFace(User user) {

        userMapper.update(null, new LambdaUpdateWrapper<User>()
                .set(User::getFaceImage, user.getFaceImage())
                .eq(User::getId, user.getId())
        );
    }

    public List<User> list() {

        return userMapper.selectList(null);
    }

    public void create(User user) {

        user.setLoginPwd(SecureUtil.md5(user.getLoginPwd()));
        userMapper.insert(user);
    }

    public List<CheckStat> checkStat(Check check) {

        List<CheckStat> checkStats = userMapper.checkStat(check);

        for (CheckStat checkStat : checkStats) {
            checkStat.setCheckDate(check.getCheckDate());
            checkStat.setCourseId(check.getCourseId());
        }

        return checkStats;
    }

    public void update(User user) {

        userMapper.updateById(user);
    }

    public void setCaptcha(String captchaId, String captcha) {
        captchaMap.put(captchaId, captcha);
    }

    public String getCaptcha(String captchaId) {
        return captchaMap.get(captchaId);
    }

    public String removeCaptcha(String captchaId) {
        return captchaMap.remove(captchaId);
    }
}
